package cn.edu.ujn.springboot.controller;

import cn.edu.ujn.dao.DeviceInfo;
import cn.edu.ujn.service.IDeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/devices")
public class DeviceInfoController {
    @Autowired
    private IDeviceInfoService deviceInfoService;
    /**
     * 添加设备
     * @param deviceInfo 设备信息
     * @return 操作结果
     */
    @PostMapping
    public String addDevice(@RequestBody DeviceInfo deviceInfo){
        int result = deviceInfoService.addDevice(deviceInfo);
        return result > 0 ? "Device added successfully!" : "Failed to add device.";
    }
    /**
     * 修改设备信息
     * @param deviceInfo 设备信息
     * @return 操作结果
     */
    @PutMapping
    public String modifyDevice(@RequestBody DeviceInfo deviceInfo) {
        int result = deviceInfoService.modifyDevice(deviceInfo);
        return result > 0 ? "Device modified successfully!" : "Failed to modify device.";
    }
    /**
     * 删除设备
     * @param deviceId 设备ID
     * @return 操作结果
     */
    @DeleteMapping("/{deviceId}")
    public String removeDevice(@PathVariable int deviceId) {
        int result = deviceInfoService.removeDevice(deviceId);
        return result > 0 ? "Device removed successfully!" : "Failed to remove device.";
    }
    /**
     * 根据ID获取设备信息
     * @param deviceId 设备ID
     * @return 设备信息
     */
    @GetMapping("/{deviceId}")
    public DeviceInfo getDeviceById(@PathVariable int deviceId) {
        return deviceInfoService.getDeviceById(deviceId);
    }
    /**
     * 获取所有设备信息
     * @return 设备列表
     */
    @GetMapping
    public List<DeviceInfo> getAllDevices() {
        return deviceInfoService.getAllDevices();
    }
}
